﻿//------------------------------------------------------------------------------
// The contents of this file are subject to the Microsoft Public License (Ms-PL)
// You may obtain a copy of the License at http://psadmin.codeplex.com/license. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, 
// WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations 
// under the License.
// 
// The initial developer is Ben Foster (ben.foster@retroviz.com)
// Copyright (c) Retroviz Limited (http://www.retroviz.com).
// All Rights Reserved.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PSAdmin.Core.Domain;
using PSAdmin.Core.Security;

namespace PSAdmin.Web.Models
{
    public class PSScriptEditViewModel
    {
        private PSScript _script;
        private ICollection<UserRole> _roles;
        private IEnumerable<Guid> _selectedRoles;
        private IEnumerable<KeyValuePair<string, Guid>> _availableRoles;

        public PSScript Script { get { return _script; } set { _script = value; } }

        public IEnumerable<Guid> SelectedRoles { get { return _selectedRoles; } }
        public IEnumerable<KeyValuePair<string, Guid>> AvailableRoles { get { return _availableRoles; } }
   
        public PSScriptEditViewModel() : this(new PSScript(), new List<UserRole>()) { }

        public PSScriptEditViewModel(PSScript script, ICollection<UserRole> roles) {
            _script = script;
            _roles = roles;
            InitRoles();
        }

        private void InitRoles() {
            _availableRoles = from r in _roles
                              select new KeyValuePair<string, Guid>(r.Name, r.Id);

            _selectedRoles = from r in _script.Roles select r.Id;
        }
    }
}
